﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager
{
    public partial class S2receive : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                inputDateFrom.Value = DateTime.Now.ToString("yyyy-MM-dd");
                inputDateTo.Value = DateTime.Now.ToString("yyyy-MM-dd");

                GVDataBind();
            }
        }

        #region GV Bind
        private void GVDataBind()
        {
            string strSQL = "SELECT * FROM (SELECT a.spnum,a.content,a.feecode,count(a.mobile) as Rnum,c.ProvinceName FROM T_Receive a WITH(NOLOCK) left join T_DH c WITH(NOLOCK) on Left(a.mobile,7)=c.DH WHERE 1=1 ";

            string strWhere = string.Empty;
            string strProvinceName = MM.DBUtility.BLSqlData.GetProvinceNamebyQuickName(txtProvince.TextValue.Trim()).Replace("省", "").Replace("壮族自治区", "").Replace("回族自治区", "").Replace("维吾尔自治区", "").Replace("特别行政区", "").Replace("自治区", "");
            if (!string.IsNullOrEmpty(strProvinceName))
                strWhere += string.Format(" AND c.ProvinceName=N'{0}'", strProvinceName);
            if (!string.IsNullOrEmpty(txtspnum.Text.Trim()))
                strWhere += string.Format(" AND a.spnum LIKE '%{0}%'", txtspnum.Text.Trim());
            if (!string.IsNullOrEmpty(txtspcontent.Text.Trim()))
                strWhere += string.Format(" AND a.content LIKE '%{0}%'", txtspcontent.Text.Trim());
            if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                strWhere += string.Format(" AND a.CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));

            strSQL += strWhere;

            strSQL += " GROUP BY a.spnum,a.content,a.feecode,c.ProvinceName) AS A";

            AspNetPager1.RecordCount = DbHelperSql.GetCount("SELECT COUNT(1) FROM (" + strSQL + ") AS B");

            string strOrder = " ORDER BY A.spnum ";

            if (AspNetPager1.CurrentPageIndex == 1)
                strSQL = string.Format(strSQL.Replace("SELECT *", "SELECT TOP {0} * "), AspNetPager1.PageSize) + strOrder;
            else
                strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL.Replace("SELECT *", string.Format("SELECT *,row_number() over({0}) rn ", strOrder)), (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            //ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + AspNetPager1.RecordCount;

            object o = DbHelperSql.GetSingle("SELECT count(*) FROM T_Receive a WITH(NOLOCK) left join T_DH c WITH(NOLOCK) on Left(a.mobile,7)=c.DH WHERE 1=1 " + strWhere);
            if (o != null)
            {
                lbRnumAll.Visible = true;
                lbRnumAll.Text = "总条数：" + o.ToString();
            }
            else lbRnumAll.Visible = false;
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GVDataBind();
        }
        #endregion

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                {
                    if (Convert.ToDateTime(inputDateFrom.Value) > Convert.ToDateTime(inputDateTo.Value))
                    {
                        MM.Common.Common.PageStartAlert(this.Page, "创建时间From必须小于等于To！");
                        return;
                    }
                }
                else
                {
                    MM.Common.Common.PageStartAlert(this.Page, "请选择查询日期！");
                    return;
                }
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
